﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase;
using GameLayer;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GameInterface;
using MainGame;

namespace GameInstrument
{
    public class Arch : Weapon
    {
        public Vector2 Velocity { get; private set; }
        private Vector2 _direct;

        public Vector2 Direct
        {
            get { return _direct; }
            set
            {
                _direct = value;
                _direct.Normalize();
                Velocity = Direct * Power;
            }
        }
        public override int Power
        {
            get
            {
                return base.Power;
            }
            set
            {
                base.Power = value;
                this.Velocity = Direct * Power;
            }
        }
        public Arch(ContentManager content)
            : base(content)
        {
            Sprite sprite0 = new Sprite(content, "Images/Sprites/Arch/0/", 6);
            Sprite sprite1 = new Sprite(content, "Images/Sprites/Arch/1/", 13);
            sprite0.MinimumIntervalIndexChange = 10;
            sprite1.MinimumIntervalIndexChange = 10;

            Fire f = new Fire(Content);
            for (int i = 0; i < 5; i++)
            {
                Fire temp = (Fire)f.Clone();
                temp.SetType(1);
                this.Fires.Add(temp);
            }
            Fires[0].Position = new Vector2(-50, 0);
            Fires[1].Position = new Vector2(50, 0);
            Fires[2].Position = new Vector2(0, -50);
            Fires[3].Position = new Vector2(-0, 50);
            Fires[4].Position = new Vector2(0, 0);

            this.Items.AddRange(new Sprite[] { sprite0, sprite1 });
        }

        private Arch(ContentManager content, List<Fire> fires, Sprite sprite0, Sprite sprite1)
            : base(content)
        {
            this.Items.AddRange(new Sprite[] { sprite0, sprite1 });
            this.Visible = this.Enable = true;
        }

        protected override void Paint(GameTime gameTime, SpriteBatch spriteBatch, object extraData)
        {
            if (Exploding)
            {
                Fires.ForEach(f =>
                {
                    Vector2 pos = f.Position;
                    f.Position += this.Position + Vector2.Normalize(this.Velocity) * this.Size.Y;
                    f.Draw(gameTime, spriteBatch, null);
                    f.Position = pos;
                });
            }
            else
            {
                base.Paint(gameTime, spriteBatch, extraData);
            }
        }

        protected override void UpdateEntitys(GameTime gameTime, object extraData)
        {
            base.UpdateEntitys(gameTime, extraData);
            Current.Update(gameTime);
            if (this.Index == 0 && Current.Index == Current.nTextures - 1)
            {
                this.Index = 1;
            }
            Current.NextFrame();
            this.Position += Velocity;
        }


        public override object Clone()
        {
            List<Fire> fires = new List<Fire>();
            this.Fires.ForEach(f => fires.Add((Fire)f.Clone()));
            return new Arch(this.Content, this.Fires, (Sprite)this.Items[0].Clone(), (Sprite)this.Items[1].Clone());
        }

        public override bool CollisionWith(Sprite other, bool pixelLevel)
        {
            if (Exploding)
            {
                foreach (var f in this.Fires)
                {
                    var pos = f.Position;
                    f.Position += this.Position;
                    bool ret = f.CollisionWith(other, pixelLevel);
                    f.Position = pos;
                    if (ret)
                    {
                        return true;
                    }
                }
            }
            return this.Items[this.Index].CollisionWith(other, pixelLevel);
        }

        public override void Explosive()
        {
            this.SoundExplosize.Play();
            this.Exploding = true;
            this.Velocity = Vector2.Normalize(this.Velocity);
        }
    }
}
